package com.he.yiliao;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;

import com.he.yiliao.DAo.UserDao;
import com.he.yiliao.adapter.RvAdapter;
import com.he.yiliao.adapter.listad;
import com.he.yiliao.adapter.upce;
import com.he.yiliao.bean.LiteratureObj;
import com.he.yiliao.bean.wenxian;
import java.util.ArrayList;
import java.util.List;
public class home extends Fragment {
    ListView listView;
    public List<wenxian>liu=new ArrayList<>();
    private List<LiteratureObj> literatureList = new ArrayList<>();
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_home, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);

        EditText etSearch = getActivity().findViewById(R.id.et_search);
        etSearch.setFocusable(false);
        etSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getActivity(), SearchActivity.class);
                startActivity(intent);
            }
        });
        new Thread(new Runnable(){
            @Override
            public void run() {
//                List<wenxian> liu=new ArrayList<>();

                UserDao userDao = new UserDao();

                try {
                   liu = userDao.selectAll();
                   for(wenxian wenxian:liu)
                   {

                   }
                    getActivity().runOnUiThread(new Runnable() {
                        public void run() {
                            listView=getActivity().findViewById(R.id.list_view);
                            listad adapter=new listad(getActivity(),R.layout.wenxain, liu);
                            listView.setAdapter(adapter);
                            //第六步：listview 的点击事件
                            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                @Override
                                public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                                   wenxian fruit= liu.get(position) ;
                                    Toast.makeText(getActivity(),fruit.getDi(),Toast.LENGTH_LONG).show();
                                    AlertDialog alertDialog2 = new AlertDialog.Builder(getActivity())
                                            .setTitle("操作选项")
                                            .setIcon(R.mipmap.ic_launcher)
                                            .setPositiveButton("查看", new DialogInterface.OnClickListener() {//添加"Yes"按钮
                                                @Override
                                                public void onClick(DialogInterface dialogInterface, int i) {
                                                    //Toast.makeText(getActivity(), "这是确定按钮", Toast.LENGTH_SHORT).show();
                                                    Intent intent = new Intent(getActivity(), RvAdapter.class);
                                                    intent.putExtra("di",fruit.getDi());
                                                    startActivity(intent);
                                                }
                                            })

                                            .setNegativeButton("下载", new DialogInterface.OnClickListener() {//添加取消
                                                @Override
                                                public void onClick(DialogInterface dialogInterface, int i) {
                                                    //Toast.makeText(getActivity(), "这是取消按钮", Toast.LENGTH_SHORT).show();
                                                   RvAdapter rvAdapter=new RvAdapter();
                                                   rvAdapter.download("http://172.18.33.51:8080/webxx/upload/"+fruit.getDi(),fruit.getDi());
                                                }
                                            })
                                            .create();
                                    alertDialog2.show();
                                }
                            });
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
